package org.nfIt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.nfIt.common.exception.CustomException;
import org.nfIt.entity.system.SysMenu;
import org.nfIt.entity.system.SysUser;
import org.nfIt.mapper.SysMenuMapper;
import org.nfIt.mapper.SysRoleMenuMapper;
import org.nfIt.service.SysMenuService;
import org.nfIt.utils.AuthContextUtil;
import org.nfIt.utils.MenuHelper;
import org.nfIt.vo.common.ResultCodeEnum;
import org.nfIt.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * ClossName:SysMenuServiceImpl
 * Pakage:org.nfIt.service.impl
 * Description
 *
 * @Author L
 * @Create 2024/6/24 19:19
 * @Version 2023.1.3
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 菜单列表查询
     */
    @Override
    public List<SysMenu> findNodes() {

        //1 查询所有菜单，返回list集合

        List<SysMenu> sysMenuslist = sysMenuMapper.findNodes();

        if (CollectionUtil.isEmpty(sysMenuslist))return null;

        //2 调用工具类的方法，把返回list集合封装要求数据格式
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuslist);
        return treeList;
    }

    /**
     * 添加菜单
     * @param sysMenu 实体
     */
    // com.atguigu.spzx.manager.service.impl#SysMenuServiceImpl
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu) ;
        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为1(半开 )
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    /**
     *  新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为1(半开 )
     * @param sysMenu
     */
    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {
        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());

        if(parentMenu != null) {

            // 将父菜单isHaIf设置为1(半开)
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;

        }
    }

    /**
     * 修改菜单
     * @param sysMenu 实体
     */
    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu) ;
    }

    // com.atguigu.spzx.manager.service.impl#SysMenuServiceImpl
    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.countByParentId(id);  // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0) {
            throw new CustomException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.deleteById(id);		// 不存在子菜单直接删除
    }

    /**
     * 查询用户可以操作的菜单
     */
    @Override
    public List<SysMenuVo> findUserMenuList() {
        //从threadload中获取当前用户信息
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();  // 获取当前登录用户的id

        //根据用户id查询可以操作的菜单
//        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId) ;

        //构建树形数据
        List<SysMenu> sysMenuList = MenuHelper.buildTree(sysMenuMapper.selectListByUserId(userId));

        List<SysMenuVo> sysMenuVoList = this.buildMenus(sysMenuList);

        return sysMenuVoList;
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();

            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }

}
